<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue实现轮播图</title>
    <style>
        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .banner {
            width: 1200px;
            height: 500px;
            background-color: pink;
            position: relative;
            margin: 0 auto;
        }

        .imgs {
            width: 100%;
            height: 100%;
            background-color: antiquewhite;
        }

        .imgs li {
            width: 1200px;
            height: 500px;
            position: absolute;
            left: 0;
            top: 0;
            font-size: 50px;
            text-align: center;
            line-height: 500px;
            color: #fff;
            transition: all 1s ease-in;
            opacity: 0.1;
        }

        .imgs li:nth-child(1) {
            background-color: aqua;
            /* z-index: 1; */
        }

        .imgs li:nth-child(2) {
            background-color: blue;
        }

        .imgs li:nth-child(3) {
            background-color: chartreuse;
        }

        .imgs li:nth-child(4) {
            background-color: darkcyan;
        }

        li.active {
            opacity: 1;
            z-index: 1;
        }

        .btns {
            width: 200px;
            height: 30px;
            position: absolute;
            left: 500px;
            bottom: 20px;
            z-index: 999;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .btns li {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #ccc;
        }

        li.btn_active {
            background-color: #fff;
        }

        .prev,
        .next {
            width: 60px;
            height: 80px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            top: 210px;
            z-index: 999;
            color: #fff;
            font-size: 60px;
            line-height: 80px;
            text-align: center;
            cursor: default;
        }

        .prev {
            left: 0;
        }

        .next {
            right: 0;
        }
    </style>
</head>

<body>
    <div class="banner">
        <ul class="imgs" ref="ban" @:mouseenter="enter" @:mouseleave="leave">
            <li :class="{active:now===0}">1</li>
            <li :class="{active:now===1}">2</li>
            <li :class="{active:now===2}">3</li>
            <li :class="{active:now===3}">4</li>
        </ul>
        <ul class="btns">
            <li :class="{btn_active:now===0}" @:click="change(0)"></li>
            <li :class="{btn_active:now===1}" @:click="change(1)"></li>
            <li :class="{btn_active:now===2}" @:click="change(2)"></li>
            <li :class="{btn_active:now===3}" @:click="change(3)"></li>
        </ul>
        <div class="prev" @:click="prev()">&lt</div>
        <div class="next" @:click="next()">&gt</div>
    </div>
    <script src="./vue.global.js"></script>
    <script>
        const { createApp } = Vue;
        createApp({
            data() {
                return {
                    now: 0,
                    interval:null,
                }
            },
            methods: {
                change(i) {
                    this.now = i;
                },
                prev() {
                    let now = this.now - 1;
                    if (now === -1) { now = 3; }
                    this.now = now;
                },
                next() {
                    let now = this.now + 1;
                    now %= 4;
                    this.now = now;
                },
                swap() {
                    let n = this.now + 1;
                    n %= 4;
                    this.now = n;
                },
                set(){
                    this.interval=setInterval(this.swap, 3000);
                },
                enter(){
                    clearInterval(this.interval);
                },
                leave(){
                    this.set();
                }
            },
            mounted() {
                this.set();
            }
        }).mount(".banner")
    </script>
</body>

</html>